package com.bw.module_home.ui.marketers

import android.app.DatePickerDialog
import android.app.TimePickerDialog
import android.widget.DatePicker
import android.widget.TimePicker
import androidx.activity.viewModels
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bw.lib_common.base.BaseActivity
import com.bw.module_home.R
import com.bw.module_home.databinding.ActivityResidencyApplicationEditBinding
import com.bw.module_home.marketer.model.ResidencyApplicationEditEntity
import com.bw.module_home.marketer.viewmodel.MarketersViewModel
import dagger.hilt.android.AndroidEntryPoint
import java.util.Calendar

@AndroidEntryPoint
@Route(path = "/module_marketers/residencyApplicationEditActivity")
class ResidencyApplicationEditActivity : BaseActivity<MarketersViewModel, ActivityResidencyApplicationEditBinding>() {
    private val viewModel: MarketersViewModel by viewModels()
    var dateAndTime = ""

    override val mLayoutResId: Int
        get() = R.layout.activity_residency_application_edit

    override fun initView() {
        super.initView()
        val calendar = Calendar.getInstance()
        val year = calendar[Calendar.YEAR]
        val month = calendar[Calendar.MONTH]
        val dayOfMonth = calendar[Calendar.DAY_OF_MONTH]
        val hour = calendar[Calendar.HOUR_OF_DAY]
        val minute = calendar[Calendar.MINUTE]

        val datePickerDialog = DatePickerDialog(
            this,
            object : DatePickerDialog.OnDateSetListener {
                override fun onDateSet(view: DatePicker?, year: Int, monthOfYear: Int, dayOfMonth: Int) {
                    // 在这里处理选择的日期
                    dateAndTime = "$year-${monthOfYear + 1}-$dayOfMonth"
                }
            },
            year, month, dayOfMonth
        )
        val timePickerDialog = TimePickerDialog(
            this,
            object : TimePickerDialog.OnTimeSetListener {
                override fun onTimeSet(p0: TimePicker?, p1: Int, p2: Int) {
                    mBinding.etEnterpriseTime.setText("$dateAndTime $p1:$p2")
                }
            },
            hour, minute, true
        )
        mBinding.ivEnterpriseTime.setOnClickListener {
            timePickerDialog.show()
            datePickerDialog.show()
        }
        mBinding.ivBack.setOnClickListener {
            finish()
        }
        mBinding.btnEnterpriseSubmit.setOnClickListener {
            val residencyApplicationEditEntity = ResidencyApplicationEditEntity(
                applicationArea = 120,
                applicationNumber = mBinding.etEnterpriseNumber.text.toString(),
                contactMethod = mBinding.etEnterprisePhone.text.toString().toLong(),
                contactName = mBinding.etEnterpriseUserName.text.toString(),
                documents = listOf("http://example.com/doc1.jpg", "http://example.com/doc2.jpg"),
                enterpriseName = mBinding.etEnterpriseName.text.toString(),
                enterpriseProfileId = 1,
                entryPersonnel = mBinding.etEnterpriseCount.text.toString().toInt(),
                entryTime = mBinding.etEnterpriseTime.text.toString(),
                id = 0,
                processingSteps = listOf(),
                status = "待审核"
            )
            viewModel.addResidencyApplication(residencyApplicationEditEntity)
        }
        mBinding.tvRecord.setOnClickListener {
            ARouter.getInstance().build("/module_marketers/residencyApplicationActivity").navigation()
        }
    }

    override fun initListener() {
        super.initListener()
        viewModel.addResidencyApplicationSuccess.observe(this) {
            showToast("添加成功")
            ARouter.getInstance().build("/module_marketers/residencyApplicationActivity").navigation()
        }
        viewModel.mErrorLiveData.observe(this) {
            showToast(it)
        }
    }
}